package com.zyh.service.impl;

import com.zyh.entity.Category;
import com.zyh.dao.CategoryMapper;
import com.zyh.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-05-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    /**
     * 获取权限树
     *
     * @return
     */
    @Override
    public List<Category> selectMenu(List<Category> categories) {
        //List<Category> categories = baseMapper.selectList(null);//查出所有菜单
        List<Category> collect = categories.stream()
                .filter((menu) -> menu.getParentId() == 0)//过滤出一级菜单
                .peek((menu)->menu.setCategoryList(selectChild(menu.getCategoryId(),categories)))//给一级分类的子菜单属性赋值。
                .collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取权限树
     *
     * @param parentId
     * @param Categories
     * @return
     */
    @Override
    public List<Category> selectChild(Long parentId, List<Category> Categories) {
        return Categories.stream().filter(menu->menu.getParentId().equals(parentId))//根据父ID过滤出对应的子菜单。
                //给当前子菜单的子菜单属性赋值
                .peek(menu->menu.setCategoryList(selectChild(menu.getCategoryId(),Categories)))//递归直至最终向上返回。
                .collect(Collectors.toList());
    }
}
